package br.com.wayloc.track;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SendSMS extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sendsms);
		
		Button bnt1 = (Button)findViewById(R.id.sendsms_send);
		bnt1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				String url = "http://187.115.169.40:31500/abracadabra/";
				TextView txt1 = (TextView)findViewById(R.id.edit_telefone);
				TextView txt2 = (TextView)findViewById(R.id.edit_mensagem);
				url = url + txt1.getText() + "/";
				
				String txt3 = Uri.encode(txt2.getText().toString());
				url = url + txt3;
				Log.e("URL",url);
				
				new RequestTask().execute(url);//resposta.enviaSMS(url);
			}
		});
	}

	//@Override
	//public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
	//	getMenuInflater().inflate(R.menu.send_sm, menu);
	//	return true;
	//}

	class RequestTask extends AsyncTask<String, String, String>{

	    @Override
	    protected String doInBackground(String... uri) {
	        HttpClient httpclient = new DefaultHttpClient();
	        HttpResponse response;
	        String responseString = null;
	        try {
	            response = httpclient.execute(new HttpGet(uri[0]));
	            StatusLine statusLine = response.getStatusLine();
	            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
	                ByteArrayOutputStream out = new ByteArrayOutputStream();
	                response.getEntity().writeTo(out);
	                out.close();
	                responseString = out.toString();
	            } else{
	                //Closes the connection.
	                response.getEntity().getContent().close();
	                throw new IOException(statusLine.getReasonPhrase());
	            }
	        } catch (ClientProtocolException e) {
	            //TODO Handle problems..
	        } catch (IOException e) {
	            //TODO Handle problems..
	        }
	        return responseString;
	    }

	    @Override
	    protected void onPostExecute(String result) {
	        super.onPostExecute(result);
	        //Do anything with response..
			AlertDialog.Builder pDialog = new AlertDialog.Builder(SendSMS.this);
			pDialog.setTitle("Resposta");
			pDialog.setMessage(result);
			pDialog.show();
			//Log.e("RESP",result);
	    }
	}
}
